package com.prjsystem.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionContext;
import com.prjsystem.common.constant.CMSConstant;
import com.prjsystem.dao.TopMenuDao;
import com.prjsystem.entity.MenuBean;
import com.prjsystem.entity.UserInfo;
import com.prjsystem.service.TopMenuService;

public class TopMenuServiceImpl implements TopMenuService {
	private TopMenuDao topMenuDao;

	public List<MenuBean> getTopMenuList(String roleId) {
		List<MenuBean> menuList = new ArrayList<MenuBean>();
		List<UserInfo> privileges = topMenuDao.searchParivileges(roleId);
		if (privileges != null && privileges.size() > 0) {
			for (UserInfo userInfo : privileges) {
				MenuBean menu = find(menuList, userInfo.getFuncname());
				// Menu List
				if (menu == null) {
					menu = new MenuBean();
					menu.setName(userInfo.getFuncname());
					menu.setUrl(userInfo.getFuncurl());
					menuList.add(menu);
					ActionContext.getContext().getSession().put(CMSConstant.MENULIST,menuList);
				}

				// Menu Hint List
				List<String> hintList = menu.getHintList();
				if (hintList == null) {
					hintList = new ArrayList<String>();
					menu.setHintList(hintList);
				}
				hintList.add(userInfo.getPrivilegesname());
			}
		}
		return menuList;
	}

	// 写一个find方法，只输出集合menuList中与MenuBean的name相同的
	private MenuBean find(List<MenuBean> menuList, String name) {
		if (menuList != null && menuList.size() > 0) {
			// for (循环变量类型 循环变量名称 : 要被遍历的对象) {循环体}
			for (MenuBean menu : menuList) {
				if (name.equals(menu.getName())) {
					return menu;
				}
			}
		}
		return null;
	}
	@Override
	public UserInfo getUserInfo(UserInfo userInfo) {
		List<UserInfo> userInfoList = topMenuDao.searchUserInfo(userInfo);
		if (userInfoList != null && userInfoList.size() > 0) {
			return userInfoList.get(0);
		}
		return null;

	}
	public TopMenuDao getTopMenuDao() {
		return topMenuDao;
	}
	public void setTopMenuDao(TopMenuDao topMenuDao) {
		this.topMenuDao = topMenuDao;
	}
}
